package com.ctsi.ssdc.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.lang.Integer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctsi.ssdc.service.LitemallAdService;
import com.ctsi.ssdc.domain.LitemallAd;

import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;


/**
 * REST controller for managing LitemallAd.
 *
 * @author ctsi-biyi-generator
 *
 */
@Api(value = "/api",tags = {"litemall-ad-controller"})
@RestController
@RequestMapping("/api")
public class LitemallAdController {

    private final Logger log = LoggerFactory.getLogger(LitemallAdController.class);

    private static final String ENTITY_NAME = "litemallAd";

    private final LitemallAdService litemallAdService;

    public LitemallAdController(LitemallAdService litemallAdService) {
        this.litemallAdService = litemallAdService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
   
    /**
     * POST  /litemallAds : Create a new litemallAd.
     *
     * @param litemallAd the litemallAd to create
     * @return the ResponseEntity with status 201 (Created) and with body the new litemallAd,
     * or with status 400 (Bad Request) if the litemallAd has already an id
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallAd",name = "litemallAd",value = "the litemallAd to create")
    })
    @ApiOperation(value = "POST  /litemallAds : create a new litemallAd.",
        notes = "POST  /litemallAds : create a new litemallAd.",httpMethod = "POST")
    @PostMapping("/litemallAds")
    public ResponseEntity<LitemallAd> createLitemallAd(@RequestBody LitemallAd litemallAd) throws URISyntaxException {
        log.debug("REST request to save LitemallAd : {}", litemallAd);
        if (litemallAd.getId() != null) {
            throw new BadRequestAlertException("A new litemallAd cannot already have an id", ENTITY_NAME, "idexists");
        }
        LitemallAd result = litemallAdService.insert(litemallAd);
        return ResponseEntity.created(new URI("/api/litemallAds" + "/" +result.getId() ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
	
    /**
     * PUT  /litemallAds : Updates an existing litemallAd.
     *
     * @param litemallAd the litemallAd to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated litemallAd,
     * or with status 400 (Bad Request) if the litemallAd is not valid,
     * or with status 500 (Internal Server Error) if the litemallAd couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallAd",
        name = "litemallAd",value = "the litemallAd to update")
    })
    @ApiOperation(value = "PUT  /litemallAds : updates an existing litemallAd.",
        notes = "PUT  /litemallAds : updates an existing litemallAd.",httpMethod = "PUT")
    @PutMapping("/litemallAds")
    public ResponseEntity<LitemallAd> updateLitemallAd(@RequestBody LitemallAd litemallAd) throws URISyntaxException {
        log.debug("REST request to update LitemallAd : {}", litemallAd);
	    if (litemallAd.getId() == null) {
	    	return createLitemallAd(litemallAd);
	    }
        LitemallAd result = litemallAdService.update(litemallAd);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * GET  /litemallAds/:id : get the "id" litemallAd.
     *
     * @param id the id of the litemallAd to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the litemallAd, or with status 404 (Not Found)
     */
//    @ApiImplicitParams({
//        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",name = "id",
//        value = "the id of the litemallAd to retrieve")
//    })
//    @ApiOperation(value = "GET  /litemallAds/id : get the id litemallAd.",
//        notes = "GET  /litemallAds/id : get the id litemallAd.",httpMethod = "GET")
//    @GetMapping("/litemallAds/{id}")
//    public ResponseEntity<LitemallAd> getLitemallAd(@PathVariable Integer id) {
//        log.debug("REST request to get LitemallAd : {}", id);
//        LitemallAd litemallAd = litemallAdService.findOne(id);
//        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(litemallAd));
//    }

    /**
    * GET  /litemallAds : get the litemallAds firstStringBaseColumn.
    *
    * @return the ResponseEntity with status 200 (OK) and the list of litemallAds in body
    */
    @ApiOperation(value = "GET  /litemallAds : get the litemallAds firstStringBaseColumn.",
                notes = "GET  /litemallAds : get the litemallAds firstStringBaseColumn.",httpMethod = "GET")
    @GetMapping("/litemallAds")
    public PageResult<LitemallAd> getLitemallAdsList(String name ,Pageable pageable) {
        log.debug("REST request to get LitemallAdsList");
        return litemallAdService.findFirstStringColumn(name ,pageable);
    }
	
    /**
     * DELETE  /litemallAds/:id : delete the "id" litemallAd.
     *
     * @param id the id of the litemallAd to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",
            name = "id",value = "the id of the litemallAd to delete")
    })
    @ApiOperation(value = "DELETE  /litemallAds/id : delete the id litemallAd.",
        notes = "DELETE  /litemallAds/id : delete the id litemallAd.",httpMethod = "DELETE")
    @DeleteMapping("/litemallAds/{id}")
    public ResponseEntity<Void> deleteLitemallAd(@PathVariable Integer id) {
        log.debug("REST request to delete LitemallAd : {}", id);
        litemallAdService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    
}
